<template>
  <div>
    <el-button type="primary" size="small" :disabled="disable" @click="addClassify">{{ title }}</el-button>
    <el-table
      :data="classifyData"
      style="width: 100%;margin-top:25px;"
      border
    >
      <el-table-column
        type="expand"
      >
        <template slot-scope="{ row ,$index}">
          <el-tag
            v-for="(item,index) in row.attr_vals"
            :key="index"
            closable
            :disable-transitions="false"
            @close="delTags(row,item)"
          >
            {{ item }}
          </el-tag>
          <el-input
            v-if="editable[$index]"
            :ref="'editableInput' + $index"
            v-model="editValue[$index]"
            class="input-new-tag"
            size="small"
            @blur="handleInputConfirm(row,$index)"
          />
          <el-button v-else class="button-new-tag" size="small" @click="showInput(row,$index)">+ New Tag</el-button>
        </template>

      </el-table-column>
      <el-table-column
        type="index"
        label="#"
        width="80"
      />
      <el-table-column
        prop="attr_name"
        label="参数名称"
      />
      <el-table-column
        label="操作"
      >
        <template slot-scope="{row}">
          <el-button type="primary" size="small" icon="el-icon-edit" @click="onEdit(row)">修改</el-button>
          <el-button type="danger" size="small" icon="el-icon-delete" @click="delClassify(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 添加按钮弹框 -->
    <el-dialog
      :title="'添加'+showWhat"
      :visible.sync="dialogVisible"
      width="50%"
    >
      <el-form ref="form" label-position="right" label-width="80px" :model="form">
        <el-form-item
          :label="showWhat"
          prop="input"
          :rules="[
            { required: true, message: '请输入参数名称', trigger: 'blur' },
            { min: 2, max: 7, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="form.input"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onSava">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改弹层 -->
    <el-dialog
      :title="'编辑'+showWhat"
      :visible.sync="editDialogVisible"
      width="50%"
    >
      <el-form ref="form1" label-position="right" label-width="80px" :model="editForm">
        <el-form-item
          :label="showWhat"
          :rules="[
            { required: true, message: '请输入参数名称', trigger: 'blur' },
            { min: 2, max: 7, message: '长度在  到 7 个字符', trigger: 'blur' }
          ]"
        >
          <el-input
            v-model="editForm.attr_name"
          />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="onEditSava">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { addClassifys, editClassifys, deleteClassifys, editPostClassifys } from '@/api/classify.js'

export default {
  name: 'DataPlan',
  props: {
    title: {
      type: String,
      required: true
    },
    disable: {
      type: Boolean,
      required: true
    },
    classifyData: {
      type: Array,
      default: () => []
    },
    catId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      inputVisible: false,
      dialogVisible: false,
      form: {
        input: ''
      },
      editDialogVisible: false,
      editForm: {},
      editable: [],
      editValue: []
    }
  },
  computed: {
    showWhat() {
      return this.title === '添加参数' ? '动态参数' : '静态属性'
    }
  },

  created() {
  },

  methods: {
    // 点击添加
    addClassify() {
      this.dialogVisible = true
    },
    // 点击newtag添加新的标签
    showInput(row, index) {
      // 让input显示
      // console.log(this.$refs.saveTagInput)
      this.$set(this.editable, index, true)
      this.$nextTick((_) => {
        var editableInput = 'editableInput' + index
        this.$refs[editableInput].$refs.input.focus()
      })
    },
    // 当输入框失去焦点和键盘弹起的时候
    async handleInputConfirm(row, index) {
      const inputValue = this.editValue[index]
      if (inputValue) {
        // 如果输入框输入了值 就push到某个地方
        row.attr_vals.push(inputValue)
        await editPostClassifys({ ...row, attr_vals: row.attr_vals.join(',') })
        this.$message.success('更新成功')
      }
      // eslint-disable-next-line require-atomic-updates
      row.inputValue = ''
      // eslint-disable-next-line require-atomic-updates
      this.$set(this.editable, index, false)
      // 然后让input消失  并清空里面的值
    },
    // 点击弹出框的确定添加动态或静态 发送请求
    async  onSava() {
      await this.$refs.form.validate()
      try {
        if (this.title === '添加参数') {
          await addClassifys(this.catId, {
            attr_name: this.form.input,
            attr_sel: 'many'
          })
        } else {
          await addClassifys(this.catId, {
            attr_name: this.form.input,
            attr_sel: 'only'
          })
        }
        this.$message.success('添加成功')
        this.dialogVisible = false
        this.$emit('onSuccess')
      } catch (err) {
        console.log(err)
      }
    },
    // 点击修改
    onEdit(data) {
      this.editForm = data
      this.editDialogVisible = true
    },
    // 点击编辑确认
    async onEditSava() {
      await this.$refs.form1.validate()
      try {
        if (this.title === '添加参数') {
          await editClassifys(this.editForm)
        } else {
          await editClassifys(this.editForm)
        }
        this.$message.success('更新成功')
        this.editDialogVisible = false
        this.$emit('onSuccess')
      } catch (err) {
        console.log(err)
      }
    },
    // 删除参数
    delClassify(data) {
      this.$confirm('此操作将永久删除该参数, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteClassifys(data)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        this.$emit('onSuccess')
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 删除标签
    async delTags(data, val) {
      const index = data.attr_vals.findIndex(item => item === val)
      data.attr_vals.splice(index, 1)
      await editPostClassifys({ ...data, attr_vals: data.attr_vals.join(',') })
      this.$message.success('更新成功')
    }
  }
}
</script>

<style scoped lang='scss'>
.el-tag + .el-tag {
    margin-left: 10px;
  }
  .button-new-tag {
    margin-left: 10px;
    height: 32px;
    line-height: 30px;
    padding-top: 0;
    padding-bottom: 0;
  }
  .input-new-tag {
    width: 90px;
    margin-left: 10px;
    vertical-align: bottom;
  }
  .tag {
    display: inline-block;
    margin-right: 5px;
  }
</style>
